package com.huajin.codetest.lock.sync;

public class SynchronizedUserTest2 {
	
	public static void main(String[] agrs) throws InterruptedException {
        Thread thread = new Thread(() -> SyncTest2.incr());
        Thread threadTwo = new Thread(() -> SyncTest2.incr());
        thread.start();
        threadTwo.start();
        thread.join();
        threadTwo.join();
        //最终打印结果是20000，如果不使用synchronized修饰，就会导致线程安全问题，输出不确定结果
        System.out.println(SyncTest2.getJ());
    }

}

class SyncTest2 {

    private static int j = 0;
    
    /**
     * 自增方法
     */
    public static synchronized void incr(){
        //临界区代码--start
        for (int i = 0; i < 10000; i++) {
            j++;
        }
        //临界区代码--end
    }

    public static int getJ() {
        return j;
    }
    
}
